/*
 * auth_console.cpp
 *
 *  Created on: Dec 21, 2013
 *      Author: killerg
 */

#include "auth_console.h"
#include "auth.h"
#include "auth_cfg.h"


#include "share/clock.h"

std::string cmd_stat(Arg &arg, void* dummy) {
    g_console->_stat = !g_console->_stat;

    return "success";
}

std::string cmd_prof(Arg &arg, void* dummy) {
    g_console->_prof = !g_console->_prof;

    return "success";
}

std::string cmd_online(Arg &arg, void* dummy) {
    std::string buffer;
    return buffer.c_str();
}

std::string cmd_shut(Arg &arg, void* dummy) {
    g_auth.svcdata->shut = true;
    return "success";
}

std::string cmd_mask(Arg &arg, void* dummy) {
    int mask = get_int64_arg(arg, "-m", 15);
    g_console->_mask_s = mask;
    return "success";
}

bool auth_console_t::init() {
    if (!console_t2::init_net())
        return false;

    if (!console_t2::start_accept(
            g_cfg->ListenConsoleIp.c_str(),
            g_cfg->ListenConsolePort)) {
        UTIL_LOG(E, "g_console start_accept fail at %s:%d",
                g_cfg->ListenConsoleIp.c_str(),
                g_cfg->ListenConsolePort);
        return false;
    }

    UTIL_LOG(I, "listening for console at %s:%d",
            g_cfg->ListenConsoleIp.c_str(),
            g_cfg->ListenConsolePort);

    bool rtv = true;
    rtv = rtv && console_t2::reg("stat", "", cmd_stat);
    rtv = rtv && console_t2::reg("online", "", cmd_online);
    rtv = rtv && console_t2::reg("prof", "", cmd_prof);
    rtv = rtv && console_t2::reg("shut", "", cmd_shut);
    rtv = rtv && console_t2::reg("mask", "m:", cmd_mask);

    _stat = true;
    _prof = true;

    _mask_f = E|W|I;
    _mask_s = E|W|I|C;

    return rtv;
}

void auth_console_t::destroy() {
    console_t2::destroy();
}

void auth_console_t::update() {
    if (g_clock->sec_jump) {
        if (_prof) {
            update_watcher();
            UTIL_LOG(C, "\n%s", watch_ss.str().c_str());
        }
        if (_stat) {

        }

    }

    console_t2::update();
}
